package com.zhu.service.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.service.edu.mapper.EduSubjectMapper;
import com.zhu.service.edu.pojo.EduSubject;
import com.zhu.service.edu.pojo.excel.ExcelSubjectData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * excel数据监听类 此类别不受spring容器管理不可以用Autowried
 */
@Slf4j
public class ExcelSubjectDataListener extends AnalysisEventListener<ExcelSubjectData> {
    private EduSubjectMapper subjectMapper;

    public ExcelSubjectDataListener(EduSubjectMapper subjectMapper) {
        this.subjectMapper=subjectMapper;
    }

    public ExcelSubjectDataListener() {

    }

    /**
     * 数据上传监听
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        /**
         * excel导入数据库的流程分析 首先将excel的数据提取组装成实体然后导入数据库
         * 1.解析excel
         */
        log.info("解析到一条记录{}",excelSubjectData);
        //处理读取出来的数据
        String levelOneTitle = excelSubjectData.getLevelOneTitle();  //一级标题
        String levelTwoTitle = excelSubjectData.getLevelTwoTitle();  //二级标题
        log.info("levelOneTitle:{}",levelOneTitle);
        log.info("levelTwoTitle:{}",levelTwoTitle);

//        ArrayList<String> levelOneTitleList=new ArrayList<>();
//        levelOneTitleList.add(levelOneTitle);
//        List<String> saveTitleJustify = this.saveTitleJustify(levelOneTitleList);
//        for (String s : saveTitleJustify) {
//            System.out.println(s);
//        }
        //2.组装数据Subject
        //判断数据是否存在
       EduSubject byTitle = this.getByTitle(levelOneTitle);
       String parentId=null;
        if(byTitle==null) {
            //组装一级类别
            EduSubject subject = new EduSubject();
            subject.setParentId("0");
            subject.setTitle(levelOneTitle);
            // 3.存入数据库
            subjectMapper.insert(subject);
            parentId= subject.getId();
        }else{
            parentId=byTitle.getId();
        }
        //判断二级数据是否存在
        EduSubject twoTitle = this.getByTwoTitle(levelTwoTitle, parentId);
        if(twoTitle==null){
            EduSubject subject = new EduSubject();
            subject.setTitle(levelTwoTitle);
            subject.setParentId(parentId);
            subjectMapper.insert(subject);
        }
    }

    /**
     * 数据上传之后 可用于结尾零散数据
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("存数剩余数据");
        log.info("剩余数据全部存储完成");
    }

    /**
     * 找到一级标题 ，辅助防止重复插入数据
     * @param title
     * @return
     */
  private EduSubject getByTitle(String title){
      LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(EduSubject::getTitle,title);
      queryWrapper.eq(EduSubject::getParentId,"0"); //一级分类
      return subjectMapper.selectOne(queryWrapper);
  }

    /**
     * 找到二级标题 ，辅助防止重复插入数据
     * @param title
     * @return
     */
    private EduSubject getByTwoTitle(String title,String parentId){
        LambdaQueryWrapper<EduSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduSubject::getTitle,title);
        queryWrapper.eq(EduSubject::getParentId,parentId); //一级分类
        return subjectMapper.selectOne(queryWrapper);
    }



  //判断list1中是否包括该元素 失败
  private static List<String> saveTitleJustify(ArrayList<String> list) {
      Iterator it = list.iterator();
      while (it.hasNext()) {
          String s = (String) it.next();
          if (!list.contains(s)) {
              list.add(s);
          }
      }
     return list;
  }
}
